<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Jobs\SendWechatMsg;
use App\Models\WechatMsgList;
use App\Models\WechatUser;

class CrontabController extends Controller
{
    public function send()
    {
        $userList = WechatUser::query()->select('id', 'name', 'openid')->where('is_subscribe', 1)->get();
        if ($userList->isEmpty()) {
            return response()->json(['code' => 400, 'msg' => '无用户']);
        }
        $model = WechatMsgList::query()->first();
        foreach ($userList->toArray() as $user) {
            SendWechatMsg::dispatch([
                'user' => $user,
                'data' => $model->toArray(),
            ]);
        }
        return response()->json(['code' => 200, 'msg' => '添加用户成功']);
    }
}
